<template>
    <div class="item-one">
        <div class="title">
            <span>优惠券核销</span>
        </div>
        <div class="container">
            <el-form :label-position="'left'" :rules="rules" ref="CodeForm" :model="CodeForm" label-width="160px">
                <el-form-item label="通道选择：" prop="choice" class="radio-form">
                    <div class="radioBox">
                        <el-radio-group v-model="CodeForm.choice">
                        <el-radio v-for="item in channelArr" :key="item.id" :label="item.channelType" border class="channelRadio" :value="item.channelName">
                        <i class="el-icon-s-shop" style="padding-right:10px; font-size:30px;vertical-align: middle;" v-if="item.img === ''"></i>
                        <img :src="item.img" alt="" v-else style="width:30px;vertical-align: middle;padding-right:10px;">
                        <span>{{ item.channelName }}</span>
                        </el-radio>
                        </el-radio-group>
                    </div>
                </el-form-item>
                <el-form-item label="请输入优惠券码：" prop="code">
                    <el-input class="ipt" v-model="CodeForm.code"></el-input>
                </el-form-item>
                <el-form-item label="请输入商户号：" prop="merChantNo" v-if="userInfo.rolesId == 5">
                    <el-input class="ipt" v-model="CodeForm.merChantNo"></el-input>
                </el-form-item>
                <el-form-item label="请选择门店：" prop="shopName"  v-if="userInfo.rolesId == 5">
                    <el-select v-model="CodeForm.shopName" class="ipt">
                        <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.shopName"
                        :value="item.shopNo">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div class="subBtn" @click="submit">
            开始核销
            </div>
        </div>
    </div>
</template>

<script>
import { mdrecord, verification, getCouponChannel } from '@/api/api'
export default {
    name: 'item-one',

    data() {
        return {
            userInfo:JSON.parse(sessionStorage.getItem('userInfo')),
            rules: {
                choice: [
                    { required: true, message: '请选择通道类型', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请输入优惠券码', trigger: 'blur' }
                ],
                merChantNo: [
                    { required: true, message: '请输入商户号', trigger: 'blur' }
                ],
                shopName: [
                    { required: true, message: '请选择门店', trigger: 'blur' }
                ],
            },
            CodeForm: {
                choice: '',
                code: '',
                merChantNo: '',
                shopName: '',
            },
            options: [],
            date: '',
            channelArr: []
        };
    },

    created() {
        this.$store.dispatch("getNewNum", 6);
        this.getCChannel()
        this.getShop()
    },

    methods: {
        // 获取核销通道
        async  getCChannel() {
            const res = await getCouponChannel()
            if(res.code === 200) {
                this.channelArr = res.data
                console.log(this.channelArr);
            } else {
                this.$message.error(res.message)
            }
        },

        // 查询门店
        async getShop() {
            const res = await mdrecord({
                page: 1,
                size: 20,
                merchantId: this.userInfo.mid
            })
            if(res.code === 200) {
                this.options = res.data.rows
            } else {
                this.$message.error(res.message)
            }
        },
        // 提交核销
        submit() {
            this.$refs.CodeForm.validate(valid => {
                // 校验
                if(!valid) return this.$message.error('请检查输入内容！')

                // 给用户提示，确定进行核销
                this.$confirm('确定是否核销','提示').then(async() => {
                    // 获取时间戳
                    let date = new Date()
                    let Y = date.getFullYear() + '-'
                    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
                    let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' '
                    let H = date.getHours() + ':'
                    let M2 = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
                    let S = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
                    this.date = Y + M + D + H + M2 + S

                    const res = await verification({
                        coupon_code: this.CodeForm.code,
                        channel_type: this.CodeForm.choice,
                        write_off_type: 0,
                        inst_no: '60000001',
                        ts: this.date,
                        merchant_no: this.CodeForm.merChantNo,
                        shop_no: this.CodeForm.shopName
                    })
                    console.log(res);
                    if(res.code === 405) {
                        sessionStorage.removeItem('token')
                        this.$router.push('/login')
                    } else {
                        if(res.code !== 200) return this.$message.error(res.message)
                        if(res.data.result_code === 'FAIL') return this.$message.error('请确认券码核销通道！')
                        this.$message.success('核销成功！')
                        this.CodeForm = {
                            choice: '',
                            code: '',
                            merChantNo: '',
                            shopName: '',
                        }
                    }
                }).catch(e => e)
            })
        }
    },
};
</script>

<style scoped lang='less'>
.title {
  background: #fff;
  padding: 10px 10px;
  font-size: 18px;
  font-weight: 500;
  display: flex;
  justify-content: space-between;
}

.container{
    padding: 60px 0;
    display: flex;
    align-items: center;
    flex-direction: column;
}

/deep/.el-form-item__label{
    line-height: 50px;
    color: #1b1b1b;
    font-size: 16px;
}

.radio-form{
    padding-bottom:20px; 
    width: 800px;
}

/deep/.radio-form .el-form-item__label{
    margin-top: 10px;
    width: 130px !important;
}

// /deep/.radio-form .el-form-item__content{
//     margin-left: 90px !important;
// }

.channelRadio{
    box-sizing: border-box;
    width: 120px;
    height: 50px;
    text-align: center;
    line-height: 30px;
    margin-top: 10px;
    margin-left: 0px !important;
    margin-right: 20px;
}

/deep/.el-radio--small.is-bordered .el-radio__label{
    padding-left: 0;
    font-size: 17px;
}

/deep/.el-radio__input{
    white-space: nowrap;
    cursor: pointer;
    outline: 0;
    line-height: 1;
    vertical-align: middle;
    display: none;
}
.ipt {
    width:540px;
    height:50px;

}

.radioBox{
    display: flex;
    flex-wrap: wrap;
}

.subBtn{
    height: 40px;
    width: 150px;
    background-color: #409eff;
    color: white;
    text-align: center;
    line-height: 40px;
    margin-top: 20px;
    margin-right: 70px;
    border-radius: 10px;
    cursor: pointer;
}

/deep/.el-input--small .el-input__inner{
  height: 45px;
}
</style>